//
//  XMFilterProjectPickerView.m
//  XiaoMiao
//
//  Created by Jim huang on 2018/5/16.
//  Copyright © 2018年 smallyou. All rights reserved.
//

#import "XMFilterProjectPickerView.h"

@interface XMFilterProjectPickerView()<UIPickerViewDataSource, UIPickerViewDelegate>
//选择器
@property (nonatomic, strong) UIPickerView *pickerView;
//二级三级关联数据
@property (nonatomic, strong) NSArray *subCateDataSource;
//一级数据
@property (nonatomic, strong) NSArray *cateDataSource;
//已选择的一级数据
@property (nonatomic, strong) ProjectSeletedModel *selectcateModel;
//已选择的二级数据
@property (nonatomic, strong) ProjectSeletedModel *selectSubcateModel;
//已选择的三级数据
@property (nonatomic, strong) ProjectSeletedModel *selectSubsubcateModel;
// 选中后的回调
@property (nonatomic, copy) FilterProjectResultBlock resultBlock;
// 取消选择的回调
@property (nonatomic, copy) FilterProjectCancelBlock cancelBlock;
@end

@implementation XMFilterProjectPickerView

+ (void)showFilterProjectPickerWithCateDataSoure:(NSArray <ProjectSeletedModel *>*)cateDataSoure
                          subCateDataSoure:(NSArray<ProjectSeletedModel *> *)subCateDataSoure
                               resultBlock:(FilterProjectResultBlock)resultBlock
                               cancelBlock:(FilterProjectCancelBlock)cancelBlock{
    XMFilterProjectPickerView * filterView = [[XMFilterProjectPickerView alloc]initWithCateDataSoure:cateDataSoure subCateDataSoure:subCateDataSoure resultBlock:resultBlock cancelBlock:cancelBlock];
    
    [filterView showWithAnimation:YES];

    
}

#pragma mark - 初始化选择器
- (instancetype)initWithCateDataSoure:(NSArray *)cateDataSoure
                      subCateDataSoure:(NSArray *)subCateDataSoure
                 resultBlock:(FilterProjectResultBlock)resultBlock
                    cancelBlock:(FilterProjectCancelBlock)cancelBlock{
    if (self = [super init]) {
        self.cateDataSource = cateDataSoure;
        self.subCateDataSource = subCateDataSoure;
        
        self.resultBlock = resultBlock;
        self.cancelBlock = cancelBlock;
        
        if (cateDataSoure.count>0) {
            self.selectcateModel = cateDataSoure[0];
        }else{
            self.selectcateModel = [[ProjectSeletedModel alloc]init];
            
        }
        
        if (subCateDataSoure.count>0) {
            
            ProjectSeletedModel * subModel = subCateDataSoure[0];
            self.selectSubcateModel = subModel;
            if (subModel.cateViews.count>0) {
                ProjectSeletedModel * subSubModel = subModel.cateViews[0];
                self.selectSubsubcateModel = subSubModel;
            }else{
                self.selectSubsubcateModel = [[ProjectSeletedModel alloc]init];
            }
            
        }else{
            self.selectSubcateModel = [[ProjectSeletedModel alloc]init];
        }
        
        if (cancelBlock) {
            [self initUIIsClear:YES];
        }else {
            [self initUIIsClear:NO];
        }
    }
    return self;
}


#pragma mark - 初始化子视图
- (void)initUIIsClear:(BOOL)isClear {
    [super initUIIsClear:isClear];
    
    // 添加时间选择器
    [self.alertView addSubview:self.pickerView];
    [self.alertView sendSubviewToBack:self.pickerView];
    [self.pickerView reloadAllComponents];
}

#pragma mark - 弹出视图方法
- (void)showWithAnimation:(BOOL)animation {
    // 1.获取当前应用的主窗口
    UIWindow *keyWindow = [[UIApplication sharedApplication] keyWindow];
    [keyWindow addSubview:self];
    if (animation) {
        // 动画前初始位置
        CGRect rect = self.alertView.frame;
        rect.origin.y = SCREEN_HEIGHT;
        self.alertView.frame = rect;
        
        // 浮现动画
        [UIView animateWithDuration:0.3 animations:^{
            CGRect rect = self.alertView.frame;
            rect.origin.y -= kDatePicHeight + kTopViewHeight + kBottomViewHeight;
            self.alertView.frame = rect;
        }];
    }
}


#pragma mark - 关闭视图方法
- (void)dismissWithAnimation:(BOOL)animation {
    // 关闭动画
    [UIView animateWithDuration:0.2 animations:^{
        CGRect rect = self.alertView.frame;
        rect.origin.y += kDatePicHeight + kTopViewHeight + kBottomViewHeight;
        self.alertView.frame = rect;
        self.backgroundView.alpha = 0;
    } completion:^(BOOL finished) {
        [self.leftBtn removeFromSuperview];
        [self.rightBtn removeFromSuperview];
        [self.titleLabel removeFromSuperview];
        [self.lineView removeFromSuperview];
        [self.topView removeFromSuperview];
        [self.pickerView removeFromSuperview];
        [self.alertView removeFromSuperview];
        [self.backgroundView removeFromSuperview];
        [self removeFromSuperview];
        
        self.leftBtn = nil;
        self.rightBtn = nil;
        self.titleLabel = nil;
        self.lineView = nil;
        self.topView = nil;
        self.pickerView = nil;
        self.alertView = nil;
        self.backgroundView = nil;
    }];
}


#pragma mark - UIPickerViewDataSource
// 1.指定pickerview有几个表盘(几列)
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
    return 3;
}


// 2.指定每个表盘上有几行数据
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
    if (component == 0) {
        // 返回省个数
        return self.cateDataSource.count;
    }
    if (component == 1) {
        // 返回市个数
        return self.subCateDataSource.count;
    }
    if (component == 2) {
        // 返回区个数
        if (self.subCateDataSource.count>0) {
            ProjectSeletedModel * model = self.subCateDataSource[0];
            return model.cateViews.count;
        }else{
            return 0;
        }

    }
    return 0;
}

#pragma mark - UIPickerViewDelegate
// 3.设置 pickerView 的 显示内容
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(nullable UIView *)view {
    
    // 设置分割线的颜色
    ((UIView *)[pickerView.subviews objectAtIndex:1]).backgroundColor = [UIColor colorWithRed:195/255.0 green:195/255.0 blue:195/255.0 alpha:1.0];
    ((UIView *)[pickerView.subviews objectAtIndex:2]).backgroundColor = [UIColor colorWithRed:195/255.0 green:195/255.0 blue:195/255.0 alpha:1.0];
    
    UIView *bgView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, (SCREEN_WIDTH) / 3, 37 * kScaleFit)];
    bgView.backgroundColor = [UIColor clearColor];
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(5 * kScaleFit, 0, (SCREEN_WIDTH) / 3 - 10 * kScaleFit, 37 * kScaleFit)];
    [bgView addSubview:label];
    label.backgroundColor = [UIColor clearColor];
    label.textAlignment = NSTextAlignmentCenter;
    //label.textColor = [UIColor redColor];
    label.font = [UIFont systemFontOfSize:16.0f * kScaleFit];
    // 字体自适应属性
    label.adjustsFontSizeToFitWidth = YES;
    // 自适应最小字体缩放比例
    label.minimumScaleFactor = 0.5f;
    if (component == 0) {
        if (self.cateDataSource.count>row) {
            ProjectSeletedModel * model = self.cateDataSource[row];
            label.text = model.name;
        }else{
            label.text = nil;
        }
    }else if (component == 1){
        if (self.subCateDataSource.count>row) {
            ProjectSeletedModel * model = self.subCateDataSource[row];

            label.text = model.name;
        }else{
            label.text = nil;
        }
    }else if (component == 2){

        if (self.selectSubcateModel.cateViews.count>row) {
                        ProjectSeletedModel * model = self.selectSubcateModel.cateViews[row];
            label.text = model.name;
        }else{
            label.text = nil;
        }
    }
    return bgView;
}

// 4.选中时回调的委托方法，在此方法中实现二三级的联动
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
    if (component == 0) { // 选择一级
        if (self.cateDataSource.count>0) {
            self.selectcateModel = self.cateDataSource[row];
            
        }else{
            self.selectcateModel = [[ProjectSeletedModel alloc]init];
        }
    }
    if (component == 1) { // 选择二级

        if (self.subCateDataSource.count>0) {
            self.selectSubcateModel = self.subCateDataSource[row];
            [self.pickerView selectRow:0 inComponent:2 animated:YES];
            [self.pickerView reloadComponent:2];
            if (self.selectSubcateModel.cateViews.count>0) {
                self.selectSubsubcateModel = self.selectSubcateModel.cateViews[0];
            }else{
                self.selectSubsubcateModel = [[ProjectSeletedModel alloc]init];
            }
        }else{
            self.selectSubcateModel = [[ProjectSeletedModel alloc]init];
            self.selectSubsubcateModel = [[ProjectSeletedModel alloc]init];
        }
    }
    if (component == 2) { // 选择区
        
        if (self.selectSubcateModel.cateViews.count>0) {
            self.selectSubsubcateModel = self.selectSubcateModel.cateViews[row];
        }else{
            
            self.selectSubsubcateModel = [[ProjectSeletedModel alloc]init];
        }
        
    }

}

// 设置行高
- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component {
    return 35.0f * kScaleFit;
}

- (NSArray *)cateDataSource {
    if (!_cateDataSource) {
        _cateDataSource = [[NSMutableArray alloc]init];
    }
    return _cateDataSource;
}

- (NSArray *)subCateDataSource {
    if (!_subCateDataSource) {
        _subCateDataSource = [NSArray array];
    }
    return _subCateDataSource;
}




#pragma mark - 确定按钮的点击事件

- (void)clickBottomBtn {
    [self clickRightBtn];
}

- (void)clickRightBtn {
    [self dismissWithAnimation:YES];
    // 点击确定按钮后，执行回调
    if(self.resultBlock) {
        NSArray *arr = @[self.selectcateModel,self.selectSubcateModel,self.selectSubsubcateModel];
        self.resultBlock(arr);
    }
}

#pragma mark - 背景视图的点击事件
- (void)didTapBackgroundView:(UITapGestureRecognizer *)sender {
    [self dismissWithAnimation:NO];
    if (self.cancelBlock) {
        self.cancelBlock(nil);
    }
}
#pragma mark - 取消按钮的点击事件
- (void)clickLeftBtn {
    [self dismissWithAnimation:YES];
    if (self.cancelBlock) {
        self.cancelBlock(nil);
    }
}
- (void)clearBtnClick {
    [self dismissWithAnimation:YES];
    if (_cancelBlock) {
        _cancelBlock(nil);
    }
}

#pragma mark - 地址选择器
- (UIPickerView *)pickerView {
    if (!_pickerView) {
        _pickerView = [[UIPickerView alloc]initWithFrame:CGRectMake(0, kTopViewHeight + 0.5, SCREEN_WIDTH, kDatePicHeight)];
        _pickerView.backgroundColor = [UIColor whiteColor];
        _pickerView.dataSource = self;
        _pickerView.delegate = self;
        _pickerView.showsSelectionIndicator = YES;
    }
    return _pickerView;
}


@end
